﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getAllCompetition();
            getAllStudent();
            getAllStaff();
        }
    }

    private void getAllStaff()
    {
        ManagerBUS manager = new ManagerBUS();
        /*
        DataSet ds = manager.getAllStaff();
        if (ds == null)
        {
            Response.Write("<script>alert('No Staff')</script>");
            return;
        }
         */
        grvStaff.DataSource = manager.getAllStaff().Tables[0];
        grvStaff.DataBind();
    }

    private void getAllStudent()
    {
        StaffBUS staff = new StaffBUS();
        /*
        DataSet ds = staff.getAllStudent();
        if (ds == null)
        {
            Response.Write("<script>alert('No Student')</script>");
            return;
        }
         */
        grvStudent.DataSource = staff.getAllStudent().Tables[0];
        grvStudent.DataBind();
    }

    private void getAllCompetition()
    {
        ManagerBUS manager = new ManagerBUS();
        /*
        DataSet ds = manager.getAllCompetition();
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
            return;
        }
         */
        grvCompetition.DataSource = manager.getAllCompetition().Tables[0];
        grvCompetition.DataBind();
    }

    protected void grvCompetition_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(grvCompetition.DataKeys[e.RowIndex].Value.ToString());
        ManagerBUS dt = new ManagerBUS();
        if (dt.delCompetition(id))
        {
            grvCompetition.EditIndex = -1;
            Response.Write("<script>alert('One Competiton deleted')</script>");
            getAllCompetition();
        }
    }

    protected void btnSeachCompetition_Click(object sender, EventArgs e)
    {
        ManagerBUS manager = new ManagerBUS();
        string name = txtSeachCompetition.Text;
        /*
        DataSet ds = manager.searchCompetition(name);
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
            return;
        }
         */
        grvCompetition.DataSource = manager.searchCompetition(name).Tables[0];
        grvCompetition.DataBind();
    }

    protected void grvCompetition_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvCompetition.EditIndex = e.NewEditIndex;
        getAllCompetition();
    }

    protected void grvCompetition_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvCompetition.EditIndex = -1;
        getAllCompetition();
    }

    protected void grvCompetition_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Competition c = new Competition();
        c.ComId = int.Parse(grvCompetition.DataKeys[e.RowIndex].Value.ToString());
        c.Name = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        c.Award = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        c.PostDate = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        c.StartDate = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        c.EndDate = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        c.Description = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        c.Quotation = ((TextBox)grvCompetition.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
        appCode.Manager m = new appCode.Manager();
        m.Id = int.Parse(((TextBox)grvCompetition.Rows[e.RowIndex].Cells[8].Controls[0]).Text);
        c.Manager = m;
        ManagerBUS manager = new ManagerBUS();

        if (manager.updateCompetition(c))
        {
            grvCompetition.EditIndex = -1;
            Response.Write("<script>alert('One Competiton updated')</script>");
            getAllCompetition();
        }
        else
        {
            Response.Write("<script>alert('Sorry, this error')</script>");
        }
    }

    protected void grvCompetition_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvCompetition.PageIndex = e.NewPageIndex;
        getAllCompetition();
    }

    protected void grvStudent_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(grvStudent.DataKeys[e.RowIndex].Value.ToString());
        StaffBUS dt = new StaffBUS();
        Student s = new Student();
        s.Id = id;
        if (dt.delStudent(s))
        {
            grvStudent.EditIndex = -1;
            Response.Write("<script>alert('One Student deleted')</script>");
            getAllStudent();
        }
    }

    protected void btnSeachStudent_Click(object sender, EventArgs e)
    {
        StaffBUS staff = new StaffBUS();
        string name = txtSeachStudent.Text;
        /*
        DataSet ds = staff.searchStudent(name);
        if (ds == null)
        {
            Response.Write("<script>alert('No Student')</script>");
            return;
        }
         */
        grvStudent.DataSource = staff.searchStudent(name).Tables[0];
        grvStudent.DataBind();
    }

    protected void grvStudent_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvStudent.EditIndex = e.NewEditIndex;
        getAllStudent();
    }

    protected void grvStudent_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvStudent.EditIndex = -1;
        getAllStudent();
    }

    protected void grvStudent_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Student s = new Student();
        s.Id = int.Parse(grvStudent.DataKeys[e.RowIndex].Value.ToString());
        s.Username = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        s.Password = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        s.Fullname = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        s.Address = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        s.Email = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        s.PhoneNumber = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        s.Major = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
        s._class1 = ((TextBox)grvStudent.Rows[e.RowIndex].Cells[8].Controls[0]).Text;

        StaffBUS staff = new StaffBUS();

        if (staff.updateStudent(s))
        {
            grvStudent.EditIndex = -1;
            Response.Write("<script>alert('One Student updated')</script>");
            getAllStudent();
        }
        else
        {
            Response.Write("<script>alert('Sorry, this error')</script>");
        }
    }

    protected void grvStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvStudent.PageIndex = e.NewPageIndex;
        getAllStudent();
    }

    protected void grvStaff_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(grvStaff.DataKeys[e.RowIndex].Value.ToString());
        ManagerBUS dt = new ManagerBUS();
        Staff s = new Staff();
        s.Id = id;
        if (dt.delStaff(s))
        {
            grvStaff.EditIndex = -1;
            Response.Write("<script>alert('One Staff deleted')</script>");
            getAllStaff();
        }
    }
    protected void btnSeachStaff_Click(object sender, EventArgs e)
    {
        ManagerBUS manager = new ManagerBUS();
        string name = txtSeachStaff.Text;
        Staff s = new Staff();
        s.Username = name;
        s.Fullname = name;
        /*
        DataSet ds = manager.searchStaff(s);
        if (ds == null)
        {
            Response.Write("<script>alert('No Staff')</script>");
            return;
        }
         */
        grvStaff.DataSource = manager.searchStaff(s).Tables[0];
        grvStaff.DataBind();
    }
    protected void grvStaff_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvStaff.EditIndex = e.NewEditIndex;
        getAllStaff();
    }
    protected void grvStaff_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvStaff.EditIndex = -1;
        getAllStaff();
    }
    protected void grvStaff_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Staff s = new Staff();
        s.Id = int.Parse(grvStaff.DataKeys[e.RowIndex].Value.ToString());
        s.Username = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        s.Password = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        s.Fullname = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        s.Address = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        s.Email = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        s.PhoneNumber = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        s.Major = ((TextBox)grvStaff.Rows[e.RowIndex].Cells[7].Controls[0]).Text;

        ManagerBUS manager = new ManagerBUS();

        if (manager.updateStaff(s))
        {
            grvStaff.EditIndex = -1;
            Response.Write("<script>alert('One Staff updated')</script>");
            getAllStaff();
        }
        else
        {
            Response.Write("<script>alert('Sorry, this error')</script>");
        }
    }
    protected void grvStaff_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvStaff.PageIndex = e.NewPageIndex;
        getAllStaff();
    }
}